﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bias_Stock.Models;
using System.IO;
using WinNemoLib;

namespace Bias_Stock.Controllers
{
    public class ExportDataController : Controller
    {
        //
        // GET: /ExportData/
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.dt = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
            return View();
        }

        [HttpPost]
        public JsonResult CartList(string dSearch)
        {
            string page = Request["page"];
            string perpage = Request["rp"];
            string qtype = Request.Form["qtype"].ToString();
            string sortBy = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            int start = (Convert.ToInt32(page) - 1) * Convert.ToInt32(perpage);

            CountCartModel rs = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = rs.List_Cart_Count(dSearch);

            if (sortorder == "asc")
            {
                list = list.OrderBy(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            if (sortorder == "desc")
            {
                list = list.OrderByDescending(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            var wOrder = from c in list select new { c._Count_date, c._Size, c._Forming,c._P1, c._P2,c._P3,c._P4,c._Top,c._Tape,c._Ng
                                                    ,c._Exp,c._Blank,c._Cart_no,c._cart_notuse,c._Type,c._Area,c._Emp,c._export,c._tag
            };

            List<CountCartModel.Cart_CNT_DS> list2 = list.Skip(Convert.ToInt32(page) - 1 * Convert.ToInt32(perpage)).Take(Convert.ToInt32(perpage)).ToList();

            Flexigrid.FlexigridCart_List flexigridObject = new Flexigrid.FlexigridCart_List();
            flexigridObject.page = 1;
            flexigridObject.total = list.Count();
            flexigridObject.rows = list2;
            return Json(flexigridObject);
        }

        [Authorize]
        public ActionResult PlanReport()
        {
            ViewBag.dt = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
            return View();
        }

        [HttpPost]
        public JsonResult CartExport(string dSearch,bool nonExport =false,bool Export = false)
        {
            string page = Request["page"];
            string perpage = Request["rp"];
            string qtype = Request.Form["qtype"].ToString();
            string sortBy = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            int start = (Convert.ToInt32(page) - 1) * Convert.ToInt32(perpage);

            CountCartModel rs = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = rs.List_Export(dSearch,nonExport,Export);

            if (sortorder == "asc")
            {
                list = list.OrderBy(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            if (sortorder == "desc")
            {
                list = list.OrderByDescending(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            var wOrder = from c in list
                         select new
                         {
                             c._Count_date,
                             c._Size,
                             c._Forming,
                             c._P1,
                             c._P2,
                             c._P3,
                             c._P4,
                             c._Top,
                             c._Tape,
                             c._Ng,
                             c._Exp
                         };

            List<CountCartModel.Cart_CNT_DS> list2 = list.Skip(Convert.ToInt32(page) - 1 * Convert.ToInt32(perpage)).Take(Convert.ToInt32(perpage)).ToList();

            Flexigrid.FlexigridCart_List flexigridObject = new Flexigrid.FlexigridCart_List();
            flexigridObject.page = 1;
            flexigridObject.total = list.Count();
            flexigridObject.rows = list2;
            return Json(flexigridObject);
        }

        public void Export_data(string dSearch, bool nonExport = false, bool Export = false)
        {
            CountCartModel rs = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = rs.List_Export(dSearch, nonExport, Export);

            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();

                CountCartModel cart = new CountCartModel();
                if (cart.Upd_Data_Exp(cnn, dSearch, nonExport) == true)
                {
                    cnn.CommitTransaction();
                }

            }

            string col = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\"";
            StringWriter sw = new StringWriter();

          if (StrUtil.IsNotEmpty(dSearch))
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\"", "Date : ", dSearch));
            }
            sw.WriteLine();
            sw.WriteLine(string.Format(col, "Date", "Size", "Forming", "P1", "P2", "P3", "P4", "Top","Tape","Ng","Expired date"));
    
            foreach (CountCartModel.Cart_CNT_DS c in list)
            {
                sw.WriteLine(string.Format(col,
                            c._Count_date,
                             c._Size,
                             c._Forming,
                             c._P1,
                             c._P2,
                             c._P3,
                             c._P4,
                             c._Top,
                             c._Tape,
                             c._Ng,
                             c._Exp
                    ));
            }
            string report = "PlanReport" + dSearch + ".csv";
            Response.AddHeader("Content-Disposition", "attachment; filename="+report);
            Response.ContentType = "text/csv";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("TIS-620");
            Response.Write(sw);
            Response.End();
        }

        public void Export_data_prd(string dSearch)
        {
            CountCartModel rs = new CountCartModel();
            List<CountCartModel.Cart_CNT_DS> list = rs.List_Cart_Count(dSearch);

            string col = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\",\"{15}\",\"{16}\",\"{17}\",\"{18}\"";
            StringWriter sw = new StringWriter();

            if (StrUtil.IsNotEmpty(dSearch))
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\"", "Date : ", dSearch));
            }
            sw.WriteLine();
            sw.WriteLine(string.Format(col, "Cart No.", "Cart not use", "Cart Type ", "DateTime", "Size", "Forming", "P1", "P2", "P3", "P4", "Top", "Tape", "Ng", "Expired date", "Blank", "Area", "UserName", "Export", "Tag"));

            foreach (CountCartModel.Cart_CNT_DS c in list)
            {
                sw.WriteLine(string.Format(col,
                         c._Cart_no, c._cart_notuse, c._Type, c._Count_date, c._Size, c._Forming
                         , c._P1, c._P2, c._P3, c._P4, c._Top, c._Tape, c._Ng
                        , c._Exp, c._Blank, c._Area, c._user, c._export, c._tag
                    ));
            }
            string report = "TireReport" + dSearch + ".csv";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + report);
            Response.ContentType = "text/csv";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("TIS-620");
            Response.Write(sw);
            Response.End();
        }
    }
}
